1. Converting Parameters to JPEG


In [1]:
from scipy.misc import imsave, toimage
from os import listdir
from os.path import basename, splitext
import glob
import numpy as np

npy_path = '../compressed-models/alexnet/npy/'
jpg_path = '../compressed-models/alexnet/jpegs/'
gif_path = '../compressed-models/alexnet/gifs/'
png_path = '../compressed-models/alexnet/pngs/'
txt_path = '../compressed-models/alexnet/txts/'

In [5]:
npy_list = glob.glob(npy_path + '*.npy')
min_max = {}

for file in npy_list:
    f = np.load(file)
    x = f.shape[0]
    y = np.prod(f.shape[1:])
    f_reshape = f.reshape(x, y)
    #f_normalized = np.round((f_reshape + 1) / 2. * 255.)    
    filename = splitext(basename(file))[0]
    #toimage(jpg_path + filename + '.jpg', f_resha)
    min_max[filename] = (f_reshape.min(), f_reshape.max())
    np.savetxt(txt_path + filename + '.txt', f_reshape)
    
#np.save(jpg_path + 'range.npy', min_max)

Load all npys and convert them to JPEG


In [ ]:
npy_list = glob.glob(npy_path + '*.npy')
min_max = {}

for file in npy_list:
    f = np.load(file)
    x = f.shape[0]
    y = np.prod(f.shape[1:])
    f_reshape = f.reshape(x, y)
    #f_normalized = np.round((f_reshape + 1) / 2. * 255.)    
    filename = splitext(basename(file))[0]
    #toimage(jpg_path + filename + '.jpg', f_resha)
    min_max[filename] = (f_reshape.min(), f_reshape.max())
    imsave(jpg_path + filename + '.jpg', f_reshape)
    
np.save(jpg_path + 'range.npy', min_max)

Load all npys and convert them to GIF


In [ ]:
npy_list = glob.glob(npy_path + '*.npy')
min_max = {}

for file in npy_list:
    f = np.load(file)
    x = f.shape[0]
    y = np.prod(f.shape[1:])
    f_reshape = f.reshape(x, y)
    #f_normalized = np.round((f_reshape + 1) / 2. * 255.)    
    filename = splitext(basename(file))[0]
    #toimage(jpg_path + filename + '.jpg', f_resha)
    min_max[filename] = (f_reshape.min(), f_reshape.max())
    imsave(gif_path + filename + '.gif', f_reshape)
    
np.save(gif_path + 'range.npy', min_max)

Load all npys and convert them to PNG


In [ ]:
npy_list = glob.glob(npy_path + '*.npy')
min_max = {}

for file in npy_list:
    f = np.load(file)
    x = f.shape[0]
    y = np.prod(f.shape[1:])
    f_reshape = f.reshape(x, y)
    #f_normalized = np.round((f_reshape + 1) / 2. * 255.)    
    filename = splitext(basename(file))[0]
    #toimage(jpg_path + filename + '.jpg', f_resha)
    min_max[filename] = (f_reshape.min(), f_reshape.max())
    imsave(png_path + filename + '.png', f_reshape)
    
np.save(png_path + 'range.npy', min_max)

2. Reference Sizes:

conv1 (96, 3, 11, 11) (96,)
conv2 (256, 48, 5, 5) (256,)
conv3 (384, 256, 3, 3) (384,)
conv4 (384, 192, 3, 3) (384,)
conv5 (256, 192, 3, 3) (256,)
fc6 (4096, 9216) (4096,)
fc7 (4096, 4096) (4096,)
fc8 (1000, 4096) (1000,)

3. Sanity Check


In [ ]:
from scipy.misc import imread

f = imread(jpg_path + 'conv1.jpg')
min_max = np.load('range.npy')
#f_normalized = (f / 255. * 2.) - 1
print f[0]
print min_max